import os
import pandas as pd
from sklearn.preprocessing import MinMaxScaler
from sklearn.model_selection import train_test_split
import numpy as np

columns = ['时间HHMM', '风速m/s', '风向', '温度℃', '湿度%', '露点温度℃', '降水量mm', '气压hPa', '能见度m']

class Data:
    def __init__(self, path):
        datas = []
        dates = []
        for i in os.listdir(path):
            df = pd.read_excel(os.path.join(path, i)).loc[:,columns]
            date = df.时间HHMM
            if ':' in date.astype('str')[0]:
                date = pd.to_datetime(date.astype('str')).dt.strftime('%H%M').astype('str').astype('int')
                dates += date.tolist()
            else:
                dates += date.astype('int').tolist()

            datas.append(df)
        new_df = pd.concat(datas, axis=0, ignore_index=True).dropna(axis=0)
        scaler = MinMaxScaler()
        x = new_df.iloc[:, 1:-1].values
        x = scaler.fit_transform(x)
        y = new_df.iloc[:,-1].values
        date = dates
        # date = pd.to_datetime(date.astype('str')).dt.strftime("%H:%M")
        self.train_date, self.test_date, self.train_X, self.test_X, self.train_y, self.test_y = train_test_split(date, x, y, test_size=0.33)



class Data2:
    def __init__(self, path=None):
        datas = []
        dates = []
        new_df = pd.read_excel('data2/combination_data.xlsx')
        scaler = MinMaxScaler()
        # x = new_df.iloc[:, 4:-3].values      # columns 1
        x = np.concatenate([new_df.iloc[:, 4:7].values, new_df.iloc[:, -7:-3].values], axis=1)
        x = scaler.fit_transform(x)
        y = new_df.iloc[:,-1].values
        date = new_df.采样点时间.str[:8] + ":" + new_df.采样点时间.str[8:]
        # date = pd.to_datetime(date.astype('str')).dt.strftime("%H:%M")
        self.train_date, self.test_date, self.train_X, self.test_X, self.train_y, self.test_y = train_test_split(date, x, y, test_size=0.33)

class Data3:
    def __init__(self, path=None):
        datas = []
        dates = []
        new_df = pd.read_excel('data3/combination_data.xlsx')
        new_df2 = pd.read_excel('data3/complement_data.xlsx')
        scaler = MinMaxScaler()
        x = new_df.iloc[:, 4:-3].values      # columns 1
        # x = np.concatenate([new_df.iloc[:, 4:7].values, new_df.iloc[:, -7:-3].values], axis=1)
        x = scaler.fit_transform(x)
        y = new_df.iloc[:,-1].values
        date = new_df.采样点时间.str[:8] + ":" + new_df.采样点时间.str[8:]

        data1 = (date, x, y)

        scaler = MinMaxScaler()
        x = new_df2.iloc[:, 1:-1].values      # columns 1
        # x = np.concatenate([new_df2.iloc[:, 1:-1].values, new_df.iloc[:, -7:-3].values], axis=1)
        x = scaler.fit_transform(x)
        y = new_df2.iloc[:,-1].values
        date = new_df2.时间HHMM.astype('str')
        date = date.str.split(' ').str[0] + ' ' + date.str.split(' ').str[1].astype(int).apply(lambda x:'%04d'%x).astype('str')
        date = date.str[:8] + ':' + date.str[8:]
        data2 = (date, x, y)
        # date = pd.to_datetime(date.astype('str')).dt.strftime("%H:%M")
        # self.train_date, self.test_date, self.train_X, self.test_X, self.train_y, self.test_y = train_test_split(date, x, y, test_size=0.33)
        self.data1 = data1
        self.data2 = data2

if __name__ == "__main__":
    # data = Data2()
    data = Data3()
